/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.player.abilities;

import static com.cmp.core.constants.InputUIDConstants.ABILITY_BUTTON_UID;
import static com.cmp.core.constants.InputUIDConstants.CURSOR_POS_UID;
import static com.cmp.core.constants.UIDConstants.BALL_UID;
import com.cmp.core.utilities.PhysicsUtility;
import com.cmp.player.controls.CharacterRigidBodyControl;
import com.cmp.player.controls.CursorSelectionControl;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;

/**
 * Ability of the carrier role.
 * @author krummer
 */
public class ChargeControl extends AbilityControl{
    private static final float CHARGE_SPEED = 15.f;
    private boolean isAbility;
    private CharacterRigidBodyControl body;
    
    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        super.setCooldown(3.f);
        super.setCastDuration(1.f);
        
        spatial.setUserData(ABILITY_BUTTON_UID, false);
        this.body = spatial.getControl(CharacterRigidBodyControl.class);
    }
    
    @Override
    public void execute() {
        //only do it without the ball
        Node ball = (Node) ((Node)this.spatial).getChild(BALL_UID);
        
        //only do when selecting a player.
        Node selected = this.getSpatial().getControl(CursorSelectionControl.class).getSelection();
        
        
        if((ball == null && !(this.isOnCooldown())) && body.isOnGround() && selected != null && (selected.getUserData("isPlayer")) != null) {
      
            super.activateCooldown();
            super.activateGCD();
            super.activateCastTime();
            
          
            //throwyourself! WEEEEEEEEEEEEEEEE!
            if(super.getSpatial().getUserData(CURSOR_POS_UID) != null) {
                    Vector3f targetPos = ((Vector3f)super.getSpatial().getUserData(CURSOR_POS_UID)).clone();
                    Vector3f myPos = getSpatial().getWorldTranslation().clone();
                    
                    // throw myself at mouse pos.
                    Vector3f throwArch = PhysicsUtility
                            .calculateVerticalLaunchSpeed(body.getGravity().y, CHARGE_SPEED, myPos, targetPos);
                    body.setLinearVelocity(throwArch);
                    

            }
        }
    }

    @Override
    protected void controlUpdate(float tpf) {
        super.controlUpdate(tpf);
        
        isAbility = super.getSpatial().getUserData(ABILITY_BUTTON_UID);
        if(isAbility) {
            execute();
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
